<template>
  <div class="relative w-full h-full">
    <PageHeader :title="$t('sellerManage.商户管理')" />
    <SearchCom
      :searchList="searchList"
      @search="toSearch"
      @reset="handleReset"
    />
    <el-table
      :data="tableData"
      style="width: 100%"
      class="cover-table mt12"
      height="calc(100% - 240px)"
      border
      v-loading="loading"
    >
      <el-table-column :prop="'name' + locale" :label="$t('sellerManage.店铺名称')" width="240"/>
      <el-table-column prop="code" :label="$t('sellerManage.店主账号')" width="200"/>
      <el-table-column prop="id" :label="$t('sellerManage.商户ID')" width="200"/>
      <el-table-column prop="phone" :label="$t('sellerManage.商户联系方式')" width="160"/>
      <el-table-column prop="settleAddress" :label="$t('sellerManage.所在地址')" width="240"/>
      <el-table-column prop="settledTime" :label="$t('sellerManage.入驻时间')" width="200">
        <template #default="{ row }">{{
          row.settledTime && moment(row.settledTime).format("YYYY-MM-DD HH:mm:ss")
        }}</template>
      </el-table-column>
      <el-table-column prop="saleCreateAmount" :label="$t('sellerManage.订单总金额')" width="180"/>
      <el-table-column prop="saleRefundDoneAmount" :label="$t('sellerManage.退货订单总金额')" width="180"/>
      <el-table-column prop="commissionRate" :label="$t('sellerManage.平台分润比例')" width="180"/>
      <el-table-column prop="saleCommissionTotal" :label="$t('sellerManage.平台分润总金额')" width="180"/>
      <el-table-column prop="skuOnSaleCount" :label="$t('sellerManage.在售商品中数量')" width="180"/>
      <el-table-column prop="settleType" :label="$t('sellerManage.入驻类型')" width="160">
        <template #default="scope">
          {{ scope.row.settleType === 1 ? t("sellerManage.个人") : t("sellerManage.企业") }}
        </template>
      </el-table-column>
      <el-table-column prop="openStatus" :label="$t('sellerManage.店铺开启状态')" width="180">
        <template #default="scope">
          <el-switch
            v-model="scope.row.openStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleOpenStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column prop="order_total_amount" label="订单总金额" /> -->
      <!-- <el-table-column
        prop="refund_order_total_amount"
        label="退货订单总金额"
      />
      <el-table-column
        prop="platform_profit_total_amount"
        label="平台分润总金额"
      /> -->
      <!-- <el-table-column prop="on_sale_product_count" label="在售商品中数量" /> -->

      <el-table-column :label="$t('sellerManage.操作')" width="500" fixed="right">
        <template #default="scope">
          <div class="operationBtnRow">
            <el-button type="text" size="small" @click="handleView(scope.row)"
              >{{ $t('sellerManage.查看') }}</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleSetRate(scope.row)"
              v-my-point="'tenant:updateCommissionRate'"
              >{{ $t('sellerManage.设置佣金比例') }}</el-button
            >

            <el-button
              class="ml0"
              type="text"
              size="small"
              @click="handleResetPassword(scope.row)"
              v-my-point="'tenant:resetPass'"
              >{{ $t('sellerManage.重置密码') }}</el-button
            >
            <el-popconfirm
              :title="$t('sellerManage.确定删除吗')"
              @confirm="handleDelete(scope.row)"
              :confirm-button-text="$t('sellerManage.确定')"
              :cancel-button-text="$t('sellerManage.取消')"
              width="300px"
            >
              <template #reference>
                <el-button class="ml0" type="text" size="small" v-my-point="'tenant:del'">{{ $t('sellerManage.删除') }}</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex justify-end mt24 absolute bottom-0 right-0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <EditDia v-model="editDiaVisible" :data="editDiaData"></EditDia>
    <SetRateDia
      v-model="setRateDiaVisible"
      :data="setRateDiaData"
      @success="getTableData"
    ></SetRateDia>
    <ResetPswDia
      v-model="resetPswDiaVisible"
      :data="resetPswData"
    ></ResetPswDia>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import EditDia from "./editDia.vue";
import Api from "@/config/api/api";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";
import SearchCom from "@/components/SearchCom.vue";
import { useRouter } from "vue-router";
import SetRateDia from "./setRateDia.vue";
import ResetPswDia from "./resetPswDia.vue";
import moment from "moment";
const { locale, t } = useI18n();

const router = useRouter();
// 店铺名称、店主账号、店主手机号、店铺状态
const searchList = [
  {
    label: t("sellerManage.店铺名称"),
    key: "name",
    type: "input",
  },
  {
    label: t("sellerManage.店主账号"),
    key: "code",
    type: "input",
  },
  {
    label: t("sellerManage.店主手机号"),
    key: "settlePhone",
    type: "input",
  },
  {
    label: t("sellerManage.店铺状态"),
    key: "openStatus",
    type: "select",
    options: [
      {
        label: t("sellerManage.停用"),
        value: "0",
      },
      {
        label: t("sellerManage.启用"),
        value: "1",
      },
    ],
  },
];
const title = ref("");
const loading = ref(true);
const editDiaVisible = ref(false);
const setRateDiaVisible = ref(false);
const editDiaData = ref({});
const setRateDiaData = ref({});
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const tableData = ref([]);
const resetPswDiaVisible = ref(false);
const resetPswData = ref({});

const handleDelete = (row) => {
  Api.deleteTenant([row.id]).then((res) => {
    if (res.status === 200) {
      currentPage.value = 1;
      getTableData();
      ElMessage.success(t("sellerManage.删除成功"));
    }
  });
};
const handleEdit = (row) => {
  router.push({
    path: "/manage_product/sellerManage/edit",
    query: {
      id: row.id,
    },
  });
};
const handleSetRate = (row) => {
  setRateDiaVisible.value = true;
  setRateDiaData.value = JSON.parse(JSON.stringify(row));
};
const handleView = (row) => {
  router.push({
    path: "/manage_product/sellerManage/detail",
    query: {
      id: row.id,
    },
  });
  // viewDiaVisible.value = true;
  // viewDiaData.value = JSON.parse(JSON.stringify(row));
};
const handleSizeChange = (size) => {
  pageSize.value = size;
  getTableData();
};
const handleCurrentChange = (page) => {
  currentPage.value = page;
  getTableData();
};

const getTableData = (params = {}) => {
  loading.value = true;
  Api.getTenantList({
    page: currentPage.value - 1,
    size: pageSize.value,
    status: 2,
    ...params,
    ["name" + locale.value]: params?.name,
    // ...searchForm.value,
  })
    .then((res) => {
      tableData.value = res.data.content;
      total.value = +res.data.totalElements;
    })
    .finally(() => {
      loading.value = false;
    });
};

const handleOpenStatusChange = async ({ openStatus, id }) => {
  Api[openStatus === 1 ? "enableTenant" : "disableTenant"](id).then((res) => {
    if (res.status === 200) {
      ElMessage.success(openStatus === 1 ? t("sellerManage.启用成功") : t("sellerManage.停用成功"));
      getTableData();
    }
  });
};

const toSearch = (params) => {
  currentPage.value = 1;
  getTableData(params);
};

const handleReset = () => {
  currentPage.value = 1;
  searchList.forEach((item) => {
    item.value = "";
  });
  getTableData();
};
const handleResetPassword = (row) => {
  resetPswData.value = JSON.parse(JSON.stringify(row));
  resetPswDiaVisible.value = true;
};
onMounted(() => {
  getTableData();
});
</script>

<style lang="scss" scoped>
.toolbar {
  width: 100%;
  height: 42px;
  margin-bottom: 5px;
  background: rgba(24, 144 ,255, 0.1);
}
.addBtn {
  height: 28px;
  margin-left: 10px;
  padding: 0 7px;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
}
.split {
  width: 1px;
  height: 12px;
  background: #ddd;
  margin: 0 5px;
}
</style>
